import reqwest from 'reqwest';
import { Notification, Message, Modal } from 'antd';
const confirm = Modal.confirm;

const Utils = {

  //线上请求地址
  //apiServer:'http://web-azp.zijincunguan.erongyun.net',
  //本地mock请求地址
  //apiServer: 'http://192.168.2.38:8888/mockjsdata/2',

  //发送请求
  //apiServer:'http://192.168.3.178:8080',
  //apiServer:'http://192.168.2.27:8080',
  //apiServer:'http://192.168.2.32:8080',
  apiServer:'',
  sendAjax: function (obj) {
    reqwest({
      url: this.apiServer + obj.url,
      contentType: obj.contentType || 'application/json;charset=UTF-8',
      method: obj.method || 'post',
      data: obj.data,
      type: obj.type || 'json',
      success: function (result) {
        if (result.code == 403) {
          confirm({
            title: result.message,
            content: '是否跳转的登录页？',
            onOk() {
              location.href = '/'
              localStorage.clear()
            },
            onCancel() {},
          });
        } else {
          obj.callback(result);
        }
        Utils.scrollWindow();
      },
      error: function (err) {
        Notification.warning({
          message: '请求失败！',
          duration: 2,
          description: '联接服务器失败，请联系客服~ o(╯□╰)o'
        })
      }
    });
  },
  // 回到页面顶端
  scrollWindow: function() {
    window.scrollTo(0, 0);
  },
  // 数组去重
  uniqueArr: function(arr) {
    var res = [];
    var json = {};
    for(var i = 0; i < arr.length; i++){
      if(!json[arr[i]]){
        res.push(arr[i]);
        json[arr[i]] = 1;
      }
    }
    return res;
  },

  formatDate: function(value) {
    if (!value) { return ''; }
    let date = new Date(value);
    let year = date.getFullYear();
    let month = (date.getMonth() + 1 > 9) ? (date.getMonth() + 1) : '0'+ (date.getMonth() + 1);
    let day = (date.getDate() > 9) ? (date.getDate()) : '0'+ (date.getDate());
    return year + "-" + month + "-" + day;
  },

  chinese: function (rule, value, callback) {
    if (!value) {
      value = '';
    }
    if (value.match(/[^\u4e00-\u9fa5]/g)) {
      callback('请输入中文名称');
    } else {
      callback();
    }
  },

  city: function (rule, value, callback) {
    if (!value) {
      value = '';
    }
    if (value.match(/[^\u4e00-\u9fa5]/g)) {
      callback('请输入城市名称');
    } else {
      callback();
    }
  },

  number: function (rule, value, callback) {
    if (!value) {
      value = '';
    }
    if (!!value && value.match(/^\+?[1-9][0-9]*$/g) || value == 0 ) {
      callback();
    } else {
      callback('请输入大于等于0的整数');
    }
  },

  integer: function (rule, value, callback) {
    if (!value) {
      value = '';
    }
    if (!!value && value.match(/^\+?[1-9]\d*$/g) || value == '') {
      callback();
    } else {
      callback('请输入大于0的整数数字');
    }
  },
  floatNum: function(rule, value, callback){
    if (!value) {
      value = '';
    }
    if (!!value && value.match(/^([1-9]\d*|0)(\.\d{1,2})?$/g) || value == 0) {
      callback();
    } else {
      callback('请输入数字，有且仅有两位小数');
    }
  },

  num: function(rule, value, callback){
    if (!value) {
      value = '';
    }
    if (!!value && value.match(/^([1-9][0-9]*)+(.[0-9]{1,2})?$/g) || value == 0) {
      callback();
    } else {
      callback('请输入大于0的数字且限小数点后两位');
    }
  },

  roomName: function(rule, value, callback){
    if(!value) {
      value = '';
    }
    if(!!value && value.match(/^.{0,20}$/g ) || value == '') {
      callback();
    }else {
      callback('房间名称不能超过20个字')
    }
  },
  phone: function (rule, value, callback) {
    if (!value) {
      value = '';
    }
    if (!!value && value.match(/^(0|86|17951)?(13[0-9]|147|15[012356789]|17[1678]|18[0-9])[0-9]{8}$/g) || value == 0) {
      callback();
    } else {
      callback('请输入正确的手机号码');
    }
  },
  phoneCode: function (rule, value, callback) {
    if(!value) {
      value = '';
    }
    if(!!value&&value.match(/^[0-9]{4}$/) || value == 0) {
      callback();
    }else {
      callback('验证码格式有误');
    }
  },
  password: function (rule, value, callback) {
    if (!value) {
      value = '';
    }
    if (!!value && value.match(/^(?![^a-zA-Z]+$)(?!\D+$).{6,15}$/g) || value == 0) {
      callback();
    } else {
      callback('6-15位字符，其中包括至少一个字母和一个数字');
    }
  },

  hireTime: function (rule, value, callback) {
    if (!value) {
      value = '';
    }
    if (!!value && value.getTime() < Date.now()) {
      callback(new Error('您不可能在这之前起租吧？'));
    } else {
      callback();
    }
  },

  name: function (rule, value, callback) {
    if (!value) {
      value = '';
    }
    if (!!value && value.match(/^[\u4e00-\u9fa5]+$/g)|| value == 0) {
      callback();
    } else {
      callback('姓名仅限于中文字符');
    }
  },

  card: function (rule, value, callback) {
    if (!value) {
      value = '';
    }
    if (!!value && value.match(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/g)|| value == 0) {
      callback();
    } else {
      callback('请输入正确的身份证号');
    }
  },

  company: function (rule, value, callback) {
    if (!value) {
      value = '';
    }
    if (!!value && value.match(/^[\u4e00-\u9fa5（）]+$/g)|| value == 0) {
      callback();
    } else {
      callback('请输入正确的物业名称');
    }
  },


}
window.Utils = Utils;

export default Utils;
